package com.androidhive.jsonparsing;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends BaseAdapter {
	 private Activity activity;
	static ImageView image;
	static View vi;
	Context Context;
    public ImageLoader imageLoader; 
	 private Map<ImageView, String> imageViews=Collections.synchronizedMap(new WeakHashMap<ImageView, String>());
	    private ArrayList<HashMap<String, String>> data;
	    private static LayoutInflater inflater=null;
	    static MemoryCache memoryCache=new MemoryCache();
	    
		    
	    public ListViewAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
	        activity = a;
	        data=d;
	        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	        imageLoader=new ImageLoader(activity.getApplicationContext());
	    }
	    
		public ListViewAdapter(Context context, ArrayList<HashMap<String, String>> d) {
			Context = context;
			inflater = LayoutInflater.from(context);
			data = d;

		}

//	    public ListViewAdapter(Activity activity,
//				ArrayList<HashMap<String, String>> data) {
//			super();
//			this.activity = activity;
//			this.data = data;
//			inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//			imageLoader=new ImageLoader(activity.getApplicationContext());
//			
//		}


		public int getCount() {
	        return data.size();
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }
	  
	    
		public static Drawable LoadImageFromWeb(String url) 
	    {
			final int stub_id = R.drawable.ic_launcher;
			 image=(ImageView)vi.findViewById(R.id.list_image);
	        try 
	        {
	        	Bitmap bitmap=memoryCache.get(url);
	        	if(bitmap!=null)
	        	{
	        	image.setImageResource(stub_id);
	          
	        	}
	        	else {
	        		  InputStream is = (InputStream) new URL(url).getContent();
	  	            Drawable d = Drawable.createFromStream(is, "src name");
	  	            return d;
				}
	        } catch (Exception e) {
	            return null;
	        }
			return null;
			
			
	    }
	      
	    public View getView(int position, View convertView, ViewGroup parent) {
	              vi=convertView;
	        if(convertView==null)
	        vi = inflater.inflate(R.layout.list_item, null);
//	        TextView content= (TextView)vi.findViewById(R.id.tvcontent);
	        TextView date = (TextView)vi.findViewById(R.id.tvdate);
	        TextView title = (TextView)vi.findViewById(R.id.tvtitle);
	        image=(ImageView)vi.findViewById(R.id.list_image);
	        
	        
	        // title
	        
	        HashMap<String, String> _id = new HashMap<String, String>();
	        _id = data.get(position);

	        //Setting all values in listview
	       
//	        content.setText(_id.get("messageID"));
	        date.setText(_id.get("time"));
	        title.setText(_id.get("title"));
	    //    imageLoader.DisplayImage(_id.get(AndroidJSONParsingActivity.TAG_PICTURE), image);
	      //  image.setBackgroundDrawable(LoadImageFromWeb(_id.get(AndroidJSONParsingActivity.TAG_PICTURE)));
	        
	        return vi;
	    }
}
